'use strict';
// bmFamily 家庭
// bmPoster 海报
const db = uniCloud.database();
let response = function(code, data, msg) {
	return {
		code: code,
		data: data,
		msg: msg || '成功'
	}
}
exports.main = async (event, context) => {
	console.log(event, '——————拿到的参数')
	if (event.api == 'addPosterRecording') {
		return newAddposterLog(event) // 添加海报
	} else if (event.api == 'getposterList') {
		return getposterList(event) // 获取海报列表
	} else if (event.api == 'commitSuggest') {
		return commitSuggest(event)
	}
	return event
};

/**
 * 新增海报
 * @param {String}  authorId 创建者 
 * @param {String}  posterUrl 海报Url
 * @param {String}  familyId  家庭Id
 * @param {String}  sortNum  优先级
 * @param {String}  createTime  创建时间
 * */
let newAddposterLog = async (param) => {
	if (!param.authorId || !param.posterUrl)
		return response(20000, undefined, '参数错误')
	let collection = await db.collection('bmPoster');
	let result = await collection.add(param)
	return response(10000, result)
};

/**
 * 获取海报列表
 * @param {String}  authorId 创建者 
 * @param {String}  familyId  家庭Id
 * */
let getposterList = async (param) => {
	if (!param.authorId)
		return response(20000, undefined, '参数错误')
	let collection = await db.collection('bmPoster');
	let result = await collection.where({
		authorId: param.authorId
	}).get()
	return response(10000, result.data)
};
/**
 * 意见收集
 * @param {String}  authorId 创建者 
 * @param {String}  content  内容
 * */
var commitSuggest = async (param) => {
	if (!param.content)
		return response(20000, undefined, '参数错误')
	let collection = await db.collection('bmLog');
	let result = await collection.add({
		authorId: param.authorId,
		content: param.content
	})
	return response(10000, result.data)
};
